package com.nox.trademe.testapp.fragments;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.TabHost.TabSpec;

import com.nox.trademe.testapp.R;
import com.nox.trademe.testapp.bonus.AboutMeActivity;
import com.nox.trademe.testapp.fragments.ListMainCategoriesFragment.CategoryListener;
import com.nox.trademe.testapp.models.MainCategory;
import com.nox.trademe.testapp.utils.KeyContainer;

public class ViewSubcategoriesFragment extends Fragment {
	
	public static final String TAG = "ViewSubcategoriesFragment";
	private FragmentActivity activity;
    private FragmentTabHost host;
    private HorizontalScrollView scroll;
    private String currentCategory = new String("");
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.v(TAG, "Activity State: onCreate()");
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onAttach(Activity activity) {
		Log.v(TAG, "Activity State: onAttach()");
		// Check if activity implements CategoryListener interface
		if (!(activity instanceof CategoryListener))
			throw new ClassCastException();
		this.activity = (FragmentActivity)activity;
		super.onAttach(activity);
	}	
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		Log.v(TAG, "Activity State: onCreateView()");
		View v = inflater.inflate(R.layout.fragment_subcategories, container, false);
		setHasOptionsMenu(false);
        host = (FragmentTabHost) v.findViewById(R.id.tabHost);
        scroll = (HorizontalScrollView) v.findViewById(R.id.horizontalScrollView);
        host.setup(getActivity(), getChildFragmentManager(), android.R.id.tabcontent);

        TabSpec spec = host.newTabSpec("Luc");
        spec.setIndicator("Luc");
        host.addTab(spec, ExpandableListFragment.class, null);

		Log.v(TAG, "Activity State: onCreateView() finished");
		return v;
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		//super.onCreateOptionsMenu(menu, inflater);
		inflater.inflate(R.menu.main_left_menu, menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.action_aboutMe:
            startActivity(new Intent(activity, AboutMeActivity.class));
			return true;
		case R.id.action_exit:
			this.getActivity().finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

    public void onMainCategoryChange(MainCategory main) {
        Log.v(TAG, "onMainCategoryChange with category [" + main.getName() + "]");
        if (currentCategory != main.getName()) {
            host.setCurrentTab(0);
            host.clearAllTabs();
            TabSpec listingSpec = host.newTabSpec("Listing of "+ main.getName());
            listingSpec.setIndicator("Listing of "+ main.getName());
            Bundle cadeau = new Bundle();
            cadeau.putString(KeyContainer.listingItemViewBundle, main.getNumber());
            host.addTab(listingSpec, ListItemFragment.class, cadeau);
            for (int i = 0; i < main.getSubcategories().size(); i++) {
                TabSpec spec = host.newTabSpec(main.getSubcategories().get(i).getName());
                spec.setIndicator(main.getSubcategories().get(i).getName());
                Bundle extra = new Bundle();
                extra.putParcelable(KeyContainer.extandableViewKeyBundle, main.getSubcategories().get(i));
                host.addTab(spec, ExpandableListFragment.class, extra);
            }
            host.setCurrentTab(0);
            currentCategory = main.getName();
        }
        new Handler().postDelayed(new Runnable() {
                public void run() {
                scroll.fullScroll(HorizontalScrollView.FOCUS_LEFT);
            }
        }, 100L);
    }
}
